package babo.cd.physicalexperiment.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public abstract class BaseFragment extends Fragment  {
    protected Activity mActivity;

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化全屏
//        ImmersionBar.with(this).statusBarDarkFont(true,0.2f).init();
        EventBus.getDefault().register(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = getLayout();
        initView();
        initData();
        setListener();
        return view;
    }


    public abstract View getLayout();

    public abstract void initView();

    public abstract void initData();

    public abstract void setListener();

    public void goToNextActivity(Class activity) {
        Intent intent = new Intent(getActivity(), activity);
        getActivity().startActivity(intent);
    }

    public void goToNextActivity(Class activity,Bundle bundle) {
        Intent intent = new Intent(getActivity(), activity);
        intent.putExtra("bundle",bundle);
        startActivity(intent);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (Activity) context;
    }


    public void onPause() {
        super.onPause();

    }

    public void onResume() {
        super.onResume();
    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    /**
     * 得到屏幕宽度
     *
     * @return 宽度
     */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }


}
